// components/new-slider-bar.js
const Timer = require('../../utils/timer')
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  options: {
    addGlobalClass: true
  },

  /**
   * 组件的初始数据
   */
  data: {
    currIndex: 0,
    list: [
      {
        id: '1',
        text: '石家庄新增47例本土确诊!河北省累计本土确诊523例'
      },
      {
        id: '2',
        text: '以高质量发展为“十四五”开好局'
      }
    ],
    subjoinItem: null,
    h: 0,
    y: 0,
    total: 0,
    transitionClass: ''
  },
  observers: {
    'list' (list) {
      console.log('list 更新', list)
      this.update(list)
    }
  },
  created () {
    this.timer = new Timer({
      callBack: () => {
        this.toTransition()
      },
      time: 3000
    })
  },
  ready () {
    const query = wx.createSelectorQuery().in(this)
    query.select('#emove').boundingClientRect((rect) => {
      this.setData({
        h: rect.height
      })
    }).exec()
    this.update(this.data.list)
  },
  // show () { console.log('show') },
  // hide () { console.log('hide') },
  // detached () { console.log('detached') },
  /**
   * 组件的方法列表
   */
  methods: {
    update (list) {
      const total = list.length
      this.setData({
        currIndex: 0,
        total: total
      })
      if (total > 1) {
        this.setData({
          subjoinItem: list[0]
        })
        this.timer.start()
      } else {
        this.setData({
          subjoinItem: []
        })
        this.timer.stop()
      }
    },
    toTransition () {
      this.setData({
        transitionClass: 'transition'
      })
      this.setData({
        currIndex: this.data.currIndex + 1
      })
    },
    transitionend () {
      this.setData({
        transitionClass: ''
      })
      if (this.data.currIndex >= this.data.total ) {
        this.setData({
          currIndex: 0
        })
      }
    }
  }
})
